package com.bestv.search.management.datasyn.spider;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.dataSource.DataSourceKeyUtil;
import com.bestv.search.common.model.NetInfoHotComment;
import com.bestv.search.common.service.NetInfoHotCommentManager;
import com.bestv.search.common.util.StringConst;
import com.bestv.search.management.datasyn.spider.model.YouKuCommentJSON;
import com.bestv.search.management.datasyn.spider.model.YouKuVideoCommentsJSON;
import org.hibernate.Hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SpiderCommentsSyncer extends SpiderSyncer {

    private NetInfoHotCommentManager netInfoHotCommentManager;

    public SpiderCommentsSyncer() {
        super();
        setNetInfoHotCommentManager((NetInfoHotCommentManager) context.getBean("netInfoHotCommentManager"));
    }

    public void sync() {
        String syncMessage = null;
        try {
            status = getProgramSyncStatus(StringConst.YOUKU, 1, StringConst.COMMENTS);
            Date syncSuccessDate = null;
            if (status != null) {
                syncSuccessDate = status.getSyncSuccessTime();
            }

            // Gets file path
            String filePath = getFileFullPath(StringConst.COMMENTS, StringConst.YOUKU, syncSuccessDate,
                    StringConst.YOUKU_ALL_VIDEOS_COMMENTS_FILE);
            if (filePath != null) {
                YouKuVideoCommentsJSON commentsJSON = new YouKuVideoCommentsJSON();
                List<NetInfoHotComment> hotComments = new ArrayList<NetInfoHotComment>();
                sync(hotComments, commentsJSON, filePath);
                syncMessage = "Success sync youku comments info from system search_spider @" + new Date();
            } else {
                syncMessage = "Doesn't find any new youku comment info in system search_spider @" + new Date();
            }
        } catch (Exception e) {
            syncMessage = "Failed sync youku comments info from system search_spider @" + new Date();
            logger.error(syncMessage);
            logger.error(e);
            e.printStackTrace();
        } finally {
            // Updates sync status
            updateSyncStatus(getSyncStatus(StringConst.YOUKU, StringConst.COMMENTS, syncMessage));
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected <T, K> T convert(K sourceObject, T destObject) {
        if (sourceObject != null) {
            List<NetInfoHotComment> hotComments = new ArrayList<NetInfoHotComment>();
            YouKuVideoCommentsJSON commentsJSON = (YouKuVideoCommentsJSON) sourceObject;
            for (YouKuCommentJSON comment : commentsJSON.getComments()) {
                if (comment.getId() != null && comment.getUser() != null) {
                    NetInfoHotComment hotComment = new NetInfoHotComment();
                    hotComment.setNetId(commentsJSON.getId());
                    hotComment.setCommentsID(comment.getId());
                    hotComment.setAuthorId(comment.getUser().getId());
                    hotComment.setAuthorName(comment.getUser().getName());
                    if (comment.getContent() != null) {
                        hotComment.setContent(Hibernate.createBlob(comment.getContent().getBytes()));
                    }
                    hotComment.setPublished(comment.getPublished());
                    hotComment.setSource(StringConst.YOUKU);
                    hotComment.setUpdateTime(new Date());
                    hotComments.add(hotComment);
                }
            }
            return (T) hotComments;
        } else {
            return null;
        }
    }

    @Override
    protected <T, K> void saveOrUpdateToDB(K sourceObj, List<T> dbObj) {
        // local site code equals to local data source Key.
        String siteCode = systemCache.getLocalDataSourceKey();
        DataSourceKey key = DataSourceKeyUtil.convert(siteCode);

        @SuppressWarnings("unchecked")
        List<List<NetInfoHotComment>> list = (List<List<NetInfoHotComment>>) dbObj;
        List<NetInfoHotComment> listToSave = new ArrayList<NetInfoHotComment>();
        for (List<NetInfoHotComment> comments : list) {
            listToSave.addAll(comments);
        }

        netInfoHotCommentManager.saveOrUpdateList(key, listToSave);
    }

    public NetInfoHotCommentManager getNetInfoHotCommentManager() {
        return netInfoHotCommentManager;
    }

    public void setNetInfoHotCommentManager(NetInfoHotCommentManager netInfoHotCommentManager) {
        this.netInfoHotCommentManager = netInfoHotCommentManager;
    }
}